﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Data;

namespace TST.SharePointObjects
{
    internal class ContentTypeList : SiteObjectList
    {
        private List<string> _demoItems;

        public ContentTypeList(LicensingMode licensingMode, SPSite site, SPWeb web, Guid farmID, SharePointObjectsDatabase database)
            : base(licensingMode, site, web, farmID, database)
        {
            _demoItems = new List<string>();
            if (licensingMode == LicensingMode.Demo)
            {
                _demoItems = Globals.GetDemoContentTypes();
            }
        }

        protected override void doLoadFromWeb()
        {
            foreach (SPContentType contentType in Web.ContentTypes)
            {
                if (LicensingMode == LicensingMode.FullLicense ||
                    (LicensingMode == LicensingMode.Demo && _demoItems.Contains(contentType.Id.ToString().ToLower())))
                {
                    SiteObject ctype = new SiteObject(contentType.Id.ToString(), contentType.Name, SiteObjectType.ContentType);
                    ctype.Group = contentType.Group;
                    ctype.Url = string.Format("{0}/_layouts/ManageContentType.aspx?ctype={1}", Web.Url, contentType.Id.ToString());
                    Add(ctype);
                }
            }
        }

        protected override void doLoadFromWeb(string groupFilter, string nameSearch)
        {
            foreach (SPContentType contentType in Web.ContentTypes)
            {
                if (LicensingMode== LicensingMode.FullLicense ||
                    (LicensingMode == LicensingMode.Demo && _demoItems.Contains(contentType.Id.ToString().ToLower())))
                {
                    if ((string.IsNullOrEmpty(groupFilter) || string.Compare(contentType.Group, groupFilter, true) == 0) &&
                       (string.IsNullOrEmpty(nameSearch) || contentType.Name.ToLower().Contains(nameSearch.ToLower())))
                    {
                        SiteObject ctype = new SiteObject(contentType.Id.ToString(), contentType.Name, SiteObjectType.ContentType);
                        ctype.Group = contentType.Group;
                        ctype.Url = string.Format("{0}/_layouts/ManageContentType.aspx?ctype={1}", Web.Url, contentType.Id.ToString());
                        Add(ctype);
                    }
                }
            }
        }

        public override SiteObjectType SiteObjectType
        {
            get
            {
                return SiteObjectType.ContentType;
            }
        }
    }
}
